package com.neusoft.blog.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.blog.bean.Category;
import com.neusoft.blog.bean.RespBean;
import com.neusoft.blog.mapper.CategoryMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    public PageInfo getCategory(Integer pageNum, Integer pageSize) {
        if(pageNum != 0 && pageSize != 0){
            PageHelper.startPage(pageNum,pageSize);//开始分页
            List<Category> list =categoryMapper.getCategory();//mapper.xml文件里面不用改，还是查询全部
            //将返回值给PageInfo
            PageInfo pageInfo=new PageInfo(list);
            return pageInfo;
        }
       else {
           List<Category> list =categoryMapper.getCategory();
            PageInfo pageInfo=new PageInfo();
            pageInfo.setList(list);
            return pageInfo;
        }
    }

    public RespBean addCategory(Category category) {
        int count =categoryMapper.addCategory(category);
        if(count>0){
            return RespBean.ok("添加成功",null);
        }
        return  RespBean.error("添加失败");
    }

    public RespBean delCategory(Integer id) {
        int count =categoryMapper.delCategory(id);
        if(count>0){
            return RespBean.ok("删除成功",null);
        }
        return  RespBean.error("删除失败");
    }

    public RespBean updateCategory(Category category) {
        int count =categoryMapper.updateCategory(category);
        if(count>0){
            return RespBean.ok("修改成功",null);
        }
        return  RespBean.error("修改失败");
    }


    public RespBean multiDel(Integer[] ids) {
        int count =categoryMapper.multiDel(ids);
        if(count>0){
            return RespBean.ok("批量删除成功",null);
        }
        return  RespBean.error("批量删除失败");
    }

}
